package com.hjqjl.module_home.ui.list.recyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hjqjl.module_home.R;
import com.hjqjl.module_home.model.MultipleItemModel;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MultipleItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MultipleItemModel> itemModels;

    static class OneViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;

        public OneViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.home_tv_multipleItem1_item_title);
        }
    }

    static class TwoViewHolder extends RecyclerView.ViewHolder {
        ImageView ivIc;
        TextView tvName;

        public TwoViewHolder(View itemView) {
            super(itemView);
            ivIc = (ImageView) itemView.findViewById(R.id.home_iv_multipleItem2_item_ic);
            tvName = (TextView) itemView.findViewById(R.id.home_tv_multipleItem2_item_name);
        }
    }

    public MultipleItemAdapter(List<MultipleItemModel> fruitList) {
        itemModels = fruitList;
    }

    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_multipleitem1, parent, false));
        } else {
            return new TwoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_multipleitem2, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, int position) {
        MultipleItemModel model = itemModels.get(position);
        if (holder instanceof OneViewHolder) {
            ((OneViewHolder) holder).tvTitle.setText(model.getName());
        } else {
            ((TwoViewHolder) holder).ivIc.setImageResource(model.getImageId());
            ((TwoViewHolder) holder).tvName.setText(model.getName());
        }
    }

    @Override
    public int getItemCount() {
        return itemModels.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }
}